package org.caijin.springrabbitMQ.mq;

import com.rabbitmq.client.AMQP;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class RabbitProducer  {
    @Autowired
    private RabbitTemplate rabbitTemplate;



    public void sendDemoQueue() {
        Date date = new Date();
        String dateString = new SimpleDateFormat("YYYY-mm-DD hh:MM:ss").format(date);
        System.out.println("[demoQueue] send msg: " + dateString);
        // 第一个参数为刚刚定义的队列名称
        this.rabbitTemplate.convertAndSend("demoQueue", dateString);
    }

    public void sendDirect(){
        String message1 = "Hello World";
        String message2 = "User.log";
        String message3 = "warning";
        this.rabbitTemplate.convertAndSend("directExchange","online", message1);
        this.rabbitTemplate.convertAndSend("directExchange","log", message2);
        this.rabbitTemplate.convertAndSend("directExchange","warn", message3);
    }

    public void sendDeadMessage(){
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType("text/plain");
        Message message = new Message("message".getBytes(), messageProperties);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        rabbitTemplate.convertAndSend("TestQueue", message);
    }

    public void sendShardingMessage(){
        for (int i = 0; i < 3000; i++) {
            String message = "shardingMessage" + i;
            this.rabbitTemplate.convertAndSend("sharding_exchange",String.valueOf(i), message);
        }
    }

    public void sendFanout() {
        Date date = new Date();
        String dateString = new SimpleDateFormat("YYYY-mm-DD hh:MM:ss").format(date);
        System.out.println("[fanout] send msg: " + dateString);
        // 注意 第一个参数是交换机的名称，第二个参数是routerKey 我们不用管空着就可以，第三个是你要发送的消息
        this.rabbitTemplate.convertAndSend("fanoutExchange", "", dateString);
    }

    public void sendTopicTopicAB() {
        Date date = new Date();
        String dateString = new SimpleDateFormat("YYYY-mm-DD hh:MM:ss").format(date);
        System.out.println("[topic.msg] send msg: " + dateString);
        // 注意 第一个参数是我们交换机的名称 ，第二个参数是routerKey topic.msg，第三个是你要发送的消息
        // 这条信息将会被 topic.a  topic.b接收
        this.rabbitTemplate.convertAndSend("topicExchange", "topic.msg", dateString);
    }

    public void sendTopicTopicB() {
        Date date = new Date();
        String dateString = new SimpleDateFormat("YYYY-mm-DD hh:MM:ss").format(date);
        System.out.println("[topic.good.msg] send msg: " + dateString);
        // 注意 第一个参数是我们交换机的名称 ，第二个参数是routerKey ，第三个是你要发送的消息
        // 这条信息将会被topic.b接收
        this.rabbitTemplate.convertAndSend("topicExchange", "topic.good.msg", dateString);
    }

    public void sendTopicTopicBC() {
        Date date = new Date();
        String dateString = new SimpleDateFormat("YYYY-mm-DD hh:MM:ss").format(date);
        System.out.println("[topic.m.z] send msg: " + dateString);
        // 注意 第一个参数是我们交换机的名称 ，第二个参数是routerKey ，第三个是你要发送的消息
        // 这条信息将会被topic.b、topic.c接收
        this.rabbitTemplate.convertAndSend("topicExchange", "topic.m.z", dateString);
    }

    public void sendHeadersAB() {
       MessageProperties properties = new MessageProperties();
       properties.setContentType("text/plain");
       properties.getHeaders().put("log-level", "error");
       properties.getHeaders().put("bus-level", "product");
       Message message = new Message("hello".getBytes(), properties);


//       rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
//           @Override
//           public void confirm(CorrelationData correlationData, boolean ack, String cause) {
//               if(ack){
//                   System.out.println("confirm ack");
//               }else {
//                   System.out.println("confirm fail");
//               }
//           }
//       });
        System.out.println("[Headers] send msg: " +message);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
       rabbitTemplate.convertAndSend("HeadersExchange",  "",message);

    }
}
